package com.firmboy.staticdb;

import com.firmboy.threadPool.PoolFactory;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * count的测试类
 *
 * @author firmboy
 * @create 2018-04-17 下午12:02
 **/
public class CountTest {

    @Test
    public void countTest(){
        Count count = new Count();

        System.out.println("开始:"+count.getCount());

        count.inc();

        System.out.println("结束:"+count.getCount());

    }


    @Test
    public void threadTest(){

        ThreadPoolExecutor pool = PoolFactory.getPool();

        int num = 10000;

        CountDownLatch countDownLatch = new CountDownLatch(num);


        final Count count = new Count();


        Runnable run = new Runnable() {
            @Override
            public void run() {
                count.inc();
                countDownLatch.countDown();
            }
        };

        for (int i=0;i<num;i++){
            pool.execute(run);
        }


        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("程序结束，count的最终值："+count.getCount());

    }
}
